package com.hpf.cloud.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hpf.cloud.entity.Student;
import com.hpf.cloud.pojo.CommonResult;
import com.hpf.cloud.pojo.PageResult;
import com.hpf.cloud.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Fred
 * @since 2020-11-22
 */
@RestController
@RequestMapping("/student")
@Slf4j
@Api(tags = "学生模块")
public class StudentController{


    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询学生列表")
    public CommonResult list(@RequestParam(required = false) Integer current, @RequestParam(required = false) Integer pageSize) {
        if (current == null) {
            current = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page<Student> page = new Page<>(current, pageSize);
        page = studentService.page(page);
        return PageResult.ok(page.getRecords(),page.getTotal(),page.getPages());
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据学生id查询学生")
    public ResponseEntity<Student> getById(@PathVariable("id") String id) {
        return new ResponseEntity<>(studentService.getById(id), HttpStatus.OK);
    }

    @PostMapping(value = "/create")
    @ApiOperation(value = "新增学生")
    public ResponseEntity<Object> create(@RequestBody Student params) {
        studentService.save(params);
        return new ResponseEntity<>("created successfully", HttpStatus.OK);
    }

    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "根据学生id删除学生")
    public ResponseEntity<Object> delete(@PathVariable("id") String id) {
        studentService.removeById(id);
        return new ResponseEntity<>("deleted successfully", HttpStatus.OK);
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新学生信息")
    public ResponseEntity<Object> delete(@RequestBody Student params) {
        studentService.updateById(params);
        return new ResponseEntity<>("updated successfully", HttpStatus.OK);
    }
}
